package com.hymall.controller.admin;

import com.hymall.bean.MarketCoupon;
import com.hymall.bean.MarketCouponUser;
import com.hymall.bean.common.BasePageInfo;
import com.hymall.bean.common.BaseRespVo;
import com.hymall.bean.common.CommonData;
import com.hymall.service.admin.CouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * @author 长平居士 - rolfcui@outlook.com
 * @description 推广管理 -> 优惠券管理
 * @date 2022/7/17 9:57
 */
@RestController
@RequestMapping("admin/coupon")
public class AdminCouponController {

    @Autowired
    CouponService couponService;

    //    @GetMapping("list")
    public BaseRespVo list(String name, Short type, Short status, BasePageInfo info) {
        CommonData<MarketCoupon> data = couponService.query(name, type, status, info);
        return BaseRespVo.ok(data);
    }

    /**
     * 重写了显示优惠券列表，增加了按查看时间显示和刷新优惠券状态
     *
     * @author: 叶熠彬
     * @date: 2022/7/21 10:39
     * @param: [name, type, status, info]
     * @return: com.hymall.bean.common.BaseRespVo
     */
    @GetMapping("list")
    public BaseRespVo list2(String name, Short type, Short status, BasePageInfo info) {
        CommonData<MarketCoupon> data = couponService.query2(name, type, status, info);
        return BaseRespVo.ok(data);
    }

    /**
     * 重写了添加优惠券，增加了对参数的判断和添加、更新日期
     *
     * @author: 叶熠彬
     * @date: 2022/7/21 9:33
     * @param: [couponItem]
     * @return: com.hymall.bean.common.BaseRespVo
     */
    @PostMapping("create")
    public BaseRespVo create2(@RequestBody MarketCoupon couponItem) {
        BaseRespVo errVo = paramCheck(couponItem);
        if (errVo != null) {
            return errVo;
        }
        couponService.insert2(couponItem);
        return BaseRespVo.ok();
    }

    /**
     * 重写了编辑优惠券，增加了对参数的判断和更新时间
     *
     * @author: 叶熠彬
     * @date: 2022/7/21 10:17
     * @param: [couponItem]
     * @return: com.hymall.bean.common.BaseRespVo
     */
    @PostMapping("update")
    public BaseRespVo update2(@RequestBody MarketCoupon couponItem) {
        BaseRespVo errVo = paramCheck(couponItem);
        if (errVo != null) {
            return errVo;
        }
        couponService.update2(couponItem);
        return BaseRespVo.ok();
    }

    //    @PostMapping("create")
    public BaseRespVo create(@RequestBody MarketCoupon couponItem) {
        // 说明：前端返回值 goodsValue 为数组，数据库中则为 varchar
        // 1. 修改自动生成的 bean 使之与前端数据类型对应
        // 2. 在 Mapper.xml 文件中找到所需语句，使用自带的 TypeHandler
        // 将 #{goodsValue,jdbcType=VARCHAR}
        // 改为 #{goodsValue,javaType=Integer[],jdbcType=VARCHAR},
        couponService.insert(couponItem);
        return BaseRespVo.ok(null);
    }

    //    @PostMapping("update")
    public BaseRespVo update(@RequestBody MarketCoupon couponItem) {
        couponService.update(couponItem);
        return BaseRespVo.ok(null);
    }

    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody MarketCoupon couponItem) {
        couponService.delete(couponItem);
        return BaseRespVo.ok(null);
    }

    @GetMapping("listuser")
    public BaseRespVo listuser(Integer couponId, Integer userId, Short status, BasePageInfo info) {
        CommonData<MarketCouponUser> data = couponService.listuser(couponId, userId, status, info);
        return BaseRespVo.ok(data);
    }

    @GetMapping("read")
    public BaseRespVo read(Integer id) {
        MarketCoupon marketCoupon = couponService.read(id);
        return BaseRespVo.ok(marketCoupon);
    }

    /**
     * 优惠券信息校验
     *
     * @author: 叶熠彬
     * @date: 2022/7/21 10:15
     * @param: [couponItem]
     * @return: com.hymall.bean.common.BaseRespVo
     */
    private BaseRespVo paramCheck(@RequestBody MarketCoupon couponItem) {
        if (couponItem.getTag() == null || couponItem.getDays() == null) {
            return BaseRespVo.error(701, "参数不能为空");
        }
        if (couponItem.getTimeType() == 0 && couponItem.getDays() <= 0) {
            return BaseRespVo.error(702, "有效期不在合法范围内");
        }
        if (couponItem.getTimeType() == 1) {
            if (couponItem.getStartTime() != null && couponItem.getEndTime() != null) {
                if (couponItem.getStartTime().after(couponItem.getEndTime())) {
                    return BaseRespVo.error(702, "有效期不在合法范围内");
                }
            } else {
                return BaseRespVo.error(702, "请指定生效时间");
            }
        }
        //最低使用金额>0
        if (couponItem.getMin().compareTo(new BigDecimal(0)) == 1) {
            if (couponItem.getMin().compareTo(couponItem.getDiscount()) == -1) {
                return BaseRespVo.error(703, "优惠金额不在合法范围内");
            }
        } else {
            return BaseRespVo.error(703, "优惠金额不在合法范围内");
        }
        return null;
    }
}
